package com.jxy.bean.constant;

import com.jxy.bean.ClassFile;
import com.jxy.tool.Tool;

import java.io.DataInputStream;

public class DynamicInfo extends ConstantPool {
    public int bootstrap_method_attr_index;
    public int name_and_type_index;

    public DynamicInfo(DataInputStream dis, ClassFile cf) {
        super(ConstantPoolTag.CONSTANT_Dynamic.getTag(), cf);
        //长度u2
        bootstrap_method_attr_index = Tool.readU2(dis);
        name_and_type_index = Tool.readU2(dis);
    }

    @Override
    public String toString() {
        return ConstantPoolTag.CONSTANT_Dynamic.getType() + "{tag:" + tag + ", bootstrap_method_attr_index:" + bootstrap_method_attr_index + ", name_and_type_index:" + name_and_type_index + "}";
    }

    @Override
    public boolean valid() {
        //name_and_type_index必须指向CONSTANT_NameAndType结构,bootstrap_method_attr_index用到后面属性，暂不校验
        if (cf.getPools()[name_and_type_index].getTag() != ConstantPoolTag.CONSTANT_NameAndType.getTag()) {
            return false;
        }
        return true;
    }
}
